home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Code Resources / 3D Buttons CDEF 1.0b4 / Source / 3D Buttons CDEF source / (3D Buttons CDEF.π) / LGBDeviceIterator.cp < prev    next >
Encoding:
Text File  |  1994-07-31  |  3.6 KB  |  120 lines  |  [TEXT/MMCC]

  1. /**************************************************************************
  2.     LGBDeviceIterator
  3.     
  4.     Public domain, by Zig Zichterman.
  5.  
  6.     a class that iterates over all devices. Does NOT do fancy-schmancy
  7.     constructor/destructor things (so that I won't incur C++ runtime
  8.     overhead in my CDEF)
  9.     
  10.     07/31/94    zz    add mDeviceIsColor
  11.     07/31/94    zz    move color QD gestalt to UGBDraw::ColorQDIsPresent()
  12. **************************************************************************/
  13. #include "LGBDeviceIterator.h"
  14.  
  15. #include "UGBDraw.h"
  16.  
  17. /**************************************************************************
  18.     Init()
  19.     
  20.     Initialize a device iterator. I do the initialization here instead
  21.     of a constructor so that I don't accidentally bring in the C++
  22.     runtime engine, which is too expensive for my puny CDEF codesize.
  23. **************************************************************************/
  24. void
  25. LGBDeviceIterator::Init(const Rect &inLocalRect)
  26. {
  27.     mNextDevice = NULL;
  28.     mNeedsOldQD    = false;
  29.     mDoneOldQD    = false;
  30.  
  31.     mGlobalRect    = inLocalRect;
  32.     ::LocalToGlobal(&(topLeft(mGlobalRect)));
  33.     ::LocalToGlobal(&(botRight(mGlobalRect)));
  34.     
  35.     // do we have color QD? If not, just set up
  36.     // a couple flags and return before we
  37.     // crash the machine with color QD calls
  38.     if (!UGBDraw::ColorQDIsPresent()) {
  39.         mNeedsOldQD = true;
  40.         return;
  41.     }
  42.         
  43.     {    // is the current port a color grafport? if not,
  44.         // pretend we're a 1-bit device (because we are)
  45.         CGrafPtr    port;
  46.         ::GetPort((GrafPtr *) &port);
  47.         if ((port->portVersion & 0xC000) != 0xC000) {
  48.             // not a color grafport. Only do 1-bit
  49.             mNeedsOldQD = true;
  50.             return;    
  51.         }
  52.     }
  53.  
  54.     // we have color QD. safe to walk the device list
  55.     mNextDevice = ::GetDeviceList();
  56. }
  57.  
  58. /**************************************************************************
  59.     Next()
  60.     
  61.     Does the next device intersect our rect? If not, keep
  62.     moving through the device list until we find one that
  63.     does. Return the depth of the screen that does.
  64.     
  65.     Return 0 if we fall off the end of the list without
  66.     finding any screens that intersect our rect
  67. **************************************************************************/
  68. short
  69. LGBDeviceIterator::Next(void)
  70. {
  71.     short    depth    = 0;
  72.     
  73.     // if we're on a color-QD-challenged device,
  74.     // don't make any color QD calls
  75.     // (like walking the device list)
  76.     if (mNeedsOldQD) {
  77.         if (!mDoneOldQD) {        // this is our first call to Next()
  78.             depth = 1;            // return a 1-bit depth
  79.             mDoneOldQD = true;    // and only do this once
  80.         }
  81.         return depth;
  82.     }
  83.     // safe to call color QD now
  84.     
  85.     // does the current device intersect? If not,
  86.     // keep qlking until we find a match
  87.     for (;mNextDevice;
  88.             mNextDevice = ::GetNextDevice(mNextDevice)) {
  89.         // skip over non-monitor devices
  90.         if ((::TestDeviceAttribute(mNextDevice,screenDevice) == 0)
  91.             || (::TestDeviceAttribute(mNextDevice,screenActive) == 0)) {
  92.             continue;    // not a screen. Keep looking            
  93.         }
  94.         
  95.         Rect    deviceBounds    = (*mNextDevice)->gdRect;
  96.         Rect    intersect;
  97.         if (!::SectRect(&deviceBounds,&mGlobalRect,&intersect)) {
  98.             continue;    // no intersection. Keep looking
  99.         }
  100.         
  101.         // clip to the intersection of the device and our rect
  102.         // ClipRect() thinks in local coords, so convert
  103.         ::GlobalToLocal(&topLeft(intersect));
  104.         ::GlobalToLocal(&botRight(intersect));
  105.         ::ClipRect(&intersect);
  106.         depth = (*(*mNextDevice)->gdPMap)->pixelSize;
  107.         
  108.         // is the device color?
  109.         mDeviceIsColor = ((**mNextDevice).gdFlags & 0x0001)?true:false;
  110.  
  111.         // we're going to break out of the loop without calling
  112.         // the loop-end incrementer (the part of the for-statement
  113.         // that moves mNextDevice to the next device). So call it
  114.         // explicitly before the breakout
  115.         mNextDevice = ::GetNextDevice(mNextDevice);
  116.         break;
  117.     }
  118.     return depth;
  119. }
  120.